#include <Color.h>
#include <ColorConvert.h>
#include <LinearColorMap.h>
#include <LinearInterpolationMap.h>
#include <ValuePoint.h>
#include <LinearColorMap.h>
#include <Modulation.h>
#include <Average.h>

#define REDPIN 5
#define GREENPIN 3
#define BLUEPIN 6
LinearColorMap* colorMap;
Average* valueAverage;
Average* maxAverage;
Modulation* modulation;
int maxDistance;
int maxAverageDistance;
int modulationDistance;
int maxValue = 1000;

void setup() {
  Serial.begin(9600);
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  modulation = newModulation();
  modulation->modulationTime = 3000;
  modulation->minimum = 0.0;
  modulation->maximum = .2;
}


void loop() {
  RGB color;
  color.r = 0;
  color.g = 50;
  color.b = 0;
  color = modulate(color);
  sendColorToPin(REDPIN, color.r);
  sendColorToPin(GREENPIN, color.g);
  sendColorToPin(BLUEPIN, color.b);
  delay(100);
}

void sendColorToPin(int pin, int color) {
  int value = constrain(color, 0, 255);
  analogWrite(pin, value);
}

RGB modulate(RGB color) {
  HSV hsvColor = RGBtoHSV(color);
  Serial.print("h: ");
  Serial.print(hsvColor.h);
  Serial.print(" s: ");
  Serial.print(hsvColor.s);
  Serial.print(" v: ");
  Serial.println(hsvColor.v);
  hsvColor.v = modulation->nextModulationValue();
  color = HSVtoRGB(hsvColor);   
  return color;
}

